package com.app.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.app.entity.*;
import com.app.service.*;
import com.app.utils.MainUtils;

@Controller
@RequestMapping("/ordertab")
public class OrdertabController {

    @Resource
    UsersService userService;
    @Resource
    OrdertabService ordertabService;


    @RequestMapping("/ordertab/add")
    public String ordertabAdd() {
        return "admin/ordertab/add";
    }

    @RequestMapping(value = "/ordertab/save", method = RequestMethod.POST)
    public String ordertabSave(String order_id,
                               String price,
                               String total,
                               String uid,
                               String pid,
                               String cuid,
                               String num,
                               String content,
                               String step,
                               String updated,
                               String created,
                               String status,
                               String buyer,
                               String pname,
                               Model model) {
        Ordertab modelX = new Ordertab();
        modelX.setOrder_id(order_id);
        modelX.setPrice(price);
        modelX.setTotal(total);
        modelX.setUid(uid);
        modelX.setPid(pid);
        modelX.setCuid(cuid);
        modelX.setNum(num);
        modelX.setContent(content);
        modelX.setStep(step);
        modelX.setUpdated(updated);
        modelX.setCreated(created);
        modelX.setStatus(status);
        modelX.setBuyer(buyer);
        modelX.setPname(pname);

        ordertabService.insert(modelX);
        model.addAttribute("inpost", true);
        model.addAttribute("message", "Success!");
        return "admin/ordertab/add";
    }

    @RequestMapping("/ordertab/delete")
    public String ordertabDelete(int id) {
        ordertabService.delete(id);
        return "redirect:list";
    }
    @RequestMapping("/ordertab/deltest")
    public String deltest(int id,String ret) {
        testsService.delete(id);
        return "redirect:"+ret;
    }
    @Resource
    TestsService testsService;
    @Resource
    InfocateService infocateService;
    @RequestMapping("/ordertab/list")
    public ModelAndView list(String company,Model model,HttpSession sess) {


    	model.addAttribute("list1",infocateService.getAllInfocate() );
        List<Ordertab> ordertabList = ordertabService.getAllOrdertab();
        List<Ordertab> ordertabList1 = new LinkedList<Ordertab>();
        for (Ordertab ordertab : ordertabList) {
        	Users users  =usersService.getUsersById(Integer.parseInt(ordertab.getUid()));

        	if(company!=null){
        		if(users.getCompany()==null) continue;
        		if(!users.getCompany().equals(company)) continue;
        	}

        	if(users.getCompany()!=null){
        		users.setCompany(
        		infocateService.getInfocateById(Integer.parseInt(users.getCompany()))
        		.getTitle());

        	}
        	////ordertab.setJl(goodsService.getGoodsById(Integer.parseInt(ordertab.getPid())).getProduct_name());
				ordertab.setYg(users);

				ordertabList1.add(ordertab);

		}

        ModelAndView mav = new ModelAndView("admin/ordertab/list");
        mav.addObject("list", ordertabList1);
        return mav;
    }


    @RequestMapping(value = "/ordertab/savekq", method = RequestMethod.POST)
    public String savekq(String token,
                            String uid,
                            String qid,
                            String score,
                            String total,
                            String created,
                            String rest,
                            String describ,
                            Model model) {
        if(StringUtils.isBlank(uid)) {
            model.addAttribute("inpost", false);
            model.addAttribute("message", "管理员无需打卡");
            return "admin/ordertab/kq";
        }
        // 限制每日签到一次
        boolean clockIn = testsService.checkClockIn(uid);
        if(!clockIn) {
            model.addAttribute("inpost", false);
            model.addAttribute("message", "今天已经打过卡了");
            return "admin/ordertab/kq";
        }
        Tests modelX = new Tests();
        modelX.setToken("1");
        modelX.setUid(uid);
        modelX.setQid(qid);
        modelX.setScore(score);
       // modelX.setTotal(MainUtils.getOrderIdByUUId());
        modelX.setCreated(MainUtils.getTime());
        modelX.setRest(rest);
        modelX.setDescrib(describ);
        testsService.insert(modelX);
        if("上班".equals(rest)){
            // 发放绩效工资
            Ordertab ordertab = ordertabService.getOrdertabByUid(Integer.parseInt(uid));
            Users user = userService.getUsersById(Integer.parseInt(uid));
            Double bonus = Double.parseDouble(user.money) + MainUtils.getBonus(ordertab.getTotal());
            user.setMoney(bonus.toString());
            userService.update(user);
        }
        model.addAttribute("inpost", true);
        model.addAttribute("message", "Success!");
        return "admin/ordertab/kq";
    }

    @RequestMapping(value = "/ordertab/saveqj1", method = RequestMethod.POST)
    public String saveqj1(String token,
                            String uid,
                            String qid,
                            String score,
                            String total,
                            String created,
                            String rest,
                            String describ,
                            Model model) {
        Tests modelX = new Tests();
        modelX.setToken("2");
        modelX.setUid(uid);
        modelX.setQid(qid);
        modelX.setScore(score);
       // modelX.setTotal(MainUtils.getOrderIdByUUId());
        modelX.setCreated(MainUtils.getTime());
        modelX.setRest(rest);
        modelX.setDescrib(describ);

        testsService.insert(modelX);
        model.addAttribute("inpost", true);
        model.addAttribute("message", "Success!");
        return "admin/ordertab/qj";
    }

    @RequestMapping(value = "/ordertab/savejb1", method = RequestMethod.POST)
    public String savejb1(String token,
                            String uid,
                            String qid,
                            String score,
                            String total,
                            String created,
                            String rest,
                            String describ,
                            Model model) {
        Tests modelX = new Tests();
        modelX.setToken("3");
        modelX.setUid(uid);
        modelX.setQid(qid);
        modelX.setScore(score);
       // modelX.setTotal(MainUtils.getOrderIdByUUId());
        modelX.setCreated(MainUtils.getTime());
        modelX.setRest(rest);
        modelX.setDescrib(describ);

        testsService.insert(modelX);
        model.addAttribute("inpost", true);
        model.addAttribute("message", "Success!");
        return "admin/ordertab/jb";
    }

    @RequestMapping("/tests/delete")
    public String testsDelete(int id) {
        testsService.delete(id);
        return "redirect:list";
    }



    @RequestMapping("/ordertab/kq")
    public ModelAndView kq(String uid,HttpSession sess) {

    	  List<Tests> testsList = testsService.getAllTests();
    	  List<Tests> testsList1 = new ArrayList<Tests>();

    	  for (Tests tests : testsList) {

    		  Ordertab ord = ordertabService.getOrdertabById(Integer.parseInt(tests.getUid()));
    		  System.out.println(uid+":"+ord.getUid());
    		  if(uid!=null&&!uid.equals(ord.getUid())) continue;
    		  Users u = usersService.getUsersById(Integer.parseInt(ord.getUid()));
    		  tests.setQid(u.getCompany());
    		  tests.setOrd(ord);
    		  if(tests.getToken().equals("1"))
    		  testsList1.add(tests);
		}



        List<Ordertab> ordertabList = ordertabService.getAllOrdertab();
        List<Ordertab> ordertabList1 = new LinkedList<Ordertab>();
        for (Ordertab ordertab : ordertabList) {


        	//ordertab.setJl(goodsService.getGoodsById(Integer.parseInt(ordertab.getPid())).getProduct_name());
				ordertab.setXy(usersService.getUsersById(Integer.parseInt(ordertab.getUid())).getUsername());

				ordertabList1.add(ordertab);

		}



        ModelAndView mav = new ModelAndView("admin/ordertab/kq");
        mav.addObject("list", ordertabList1);
        mav.addObject("list1", testsList1);
        return mav;
    }

    @RequestMapping("/ordertab/qj1")
    public ModelAndView qj1(String uid,HttpSession sess) {

    	  List<Tests> testsList = testsService.getAllTests();
    	  List<Tests> testsList1 = new ArrayList<Tests>();

    	  for (Tests tests : testsList) {
    		  Ordertab ord = ordertabService.getOrdertabById(Integer.parseInt(tests.getUid()));
    		  if(uid!=null&&!ord.getUid().equals(uid)) continue;
    		  Users u = usersService.getUsersById(Integer.parseInt(ord.getUid()));
    		  tests.setQid(u.getCompany());
    		  tests.setOrd(ord);
    		  if(tests.getToken().equals("2"))
    		  testsList1.add(tests);
		}



        List<Ordertab> ordertabList = ordertabService.getAllOrdertab();
        List<Ordertab> ordertabList1 = new LinkedList<Ordertab>();
        for (Ordertab ordertab : ordertabList) {


        	//ordertab.setJl(goodsService.getGoodsById(Integer.parseInt(ordertab.getPid())).getProduct_name());
				ordertab.setXy(usersService.getUsersById(Integer.parseInt(ordertab.getUid())).getUsername());

				ordertabList1.add(ordertab);

		}



        ModelAndView mav = new ModelAndView("admin/ordertab/qj");
        mav.addObject("list", ordertabList1);
        mav.addObject("list1", testsList1);
        return mav;
    }


    @RequestMapping("/ordertab/jb")
    public ModelAndView jb(String uid,HttpSession sess) {

    	  List<Tests> testsList = testsService.getAllTests();
    	  List<Tests> testsList1 = new ArrayList<Tests>();

    	  for (Tests tests : testsList) {
    		  Ordertab ord = ordertabService.getOrdertabById(Integer.parseInt(tests.getUid()));
    		  if(uid!=null&&!ord.getUid().equals(uid)) continue;
    		  Users u = usersService.getUsersById(Integer.parseInt(ord.getUid()));
    		  tests.setQid(u.getCompany());
    		  tests.setOrd(ord);
    		  if(tests.getToken().equals("3"))
    		  testsList1.add(tests);
		}



        List<Ordertab> ordertabList = ordertabService.getAllOrdertab();
        List<Ordertab> ordertabList1 = new LinkedList<Ordertab>();
        for (Ordertab ordertab : ordertabList) {


        	//ordertab.setJl(goodsService.getGoodsById(Integer.parseInt(ordertab.getPid())).getProduct_name());
				ordertab.setXy(usersService.getUsersById(Integer.parseInt(ordertab.getUid())).getUsername());

				ordertabList1.add(ordertab);

		}



        ModelAndView mav = new ModelAndView("admin/ordertab/jb");
        mav.addObject("list", ordertabList1);
        mav.addObject("list1", testsList1);
        return mav;
    }

    @RequestMapping("/ordertab/qj")
    public ModelAndView qj(HttpSession sess) {
        List<Ordertab> ordertabList = ordertabService.getAllOrdertab();
        List<Ordertab> ordertabList1 = new LinkedList<Ordertab>();
        for (Ordertab ordertab : ordertabList) {


        	//ordertab.setJl(goodsService.getGoodsById(Integer.parseInt(ordertab.getPid())).getProduct_name());
				ordertab.setXy(usersService.getUsersById(Integer.parseInt(ordertab.getUid())).getUsername());

				ordertabList1.add(ordertab);

		}

        ModelAndView mav = new ModelAndView("admin/ordertab/qj");
        mav.addObject("list", ordertabList1);
        return mav;
    }

    @Resource
    UsersService usersService;

    @RequestMapping("/ordertab/my")
    public ModelAndView my(HttpSession sess) {
        List<Ordertab> ordertabList = ordertabService.getAllOrdertab();
        List<Ordertab> ordertabList1 = new LinkedList<Ordertab>();
        for (Ordertab ordertab : ordertabList) {
			if(sess.getAttribute("userid").toString().equals(ordertab.getUid())){

				//ordertab.setJl(goodsService.getGoodsById(Integer.parseInt(ordertab.getPid())).getProduct_name());
				ordertab.setXy(usersService.getUsersById(Integer.parseInt(ordertab.getUid())).getUsername());

				ordertabList1.add(ordertab);
			}
		}
        System.out.println(ordertabList.size());
        ModelAndView mav = new ModelAndView("admin/ordertab/my");
        mav.addObject("list", ordertabList1);
        return mav;
    }


    @RequestMapping("/ordertab/cj")
    public ModelAndView cj(HttpSession sess) {
        List<Ordertab> ordertabList = ordertabService.getAllOrdertab();
        List<Ordertab> ordertabList1 = new LinkedList<Ordertab>();
        for (Ordertab ordertab : ordertabList) {
			//if(sess.getAttribute("userid").toString().equals(ordertab.getUid())){

				//ordertab.setJl(goodsService.getGoodsById(Integer.parseInt(ordertab.getPid())).getProduct_name());
        	Users users  =usersService.getUsersById(Integer.parseInt(ordertab.getUid()));


        	if(users.getCompany()!=null){
        		users.setCompany(
        		infocateService.getInfocateById(Integer.parseInt(users.getCompany()))
        		.getTitle());

        	}
        	////ordertab.setJl(goodsService.getGoodsById(Integer.parseInt(ordertab.getPid())).getProduct_name());
				ordertab.setYg(users);


				ordertabList1.add(ordertab);
			//}
		}
        System.out.println(ordertabList.size());
        ModelAndView mav = new ModelAndView("admin/ordertab/cj");
        mav.addObject("list", ordertabList1);
        return mav;
    }

    @RequestMapping("/ordertab/pj")
    public ModelAndView pj(HttpSession sess) {
        List<Ordertab> ordertabList = ordertabService.getAllOrdertab();
        List<Ordertab> ordertabList1 = new LinkedList<Ordertab>();
        for (Ordertab ordertab : ordertabList) {
			//if(sess.getAttribute("userid").toString().equals(ordertab.getUid())){

				//ordertab.setJl(goodsService.getGoodsById(Integer.parseInt(ordertab.getPid())).getProduct_name());
				ordertab.setXy(usersService.getUsersById(Integer.parseInt(ordertab.getUid())).getUsername());

				ordertabList1.add(ordertab);
			//}
		}
        System.out.println(ordertabList.size());
        ModelAndView mav = new ModelAndView("admin/ordertab/pj");
        mav.addObject("list", ordertabList1);
        return mav;
    }


    @RequestMapping("/ordertab/zg")
    public ModelAndView zg(HttpSession sess) {
        List<Ordertab> ordertabList = ordertabService.getAllOrdertab();
        List<Ordertab> ordertabList1 = new LinkedList<Ordertab>();
        for (Ordertab ordertab : ordertabList) {
			if(sess.getAttribute("userid").toString().equals(ordertab.getCuid())){

				//ordertab.setJl(goodsService.getGoodsById(Integer.parseInt(ordertab.getPid())).getProduct_name());
				ordertab.setXy(usersService.getUsersById(Integer.parseInt(ordertab.getUid())).getUsername());
				if(ordertab.getCuid()!=null&&!ordertab.getCuid().equals("")){
					ordertab.setCuid(usersService.getUsersById(Integer.parseInt(ordertab.getCuid())).getUsername());
				}

				ordertabList1.add(ordertab);
			}
		}
        System.out.println(ordertabList.size());
        ModelAndView mav = new ModelAndView("admin/ordertab/zg");
        mav.addObject("list", ordertabList1);
        mav.addObject("list1", usersService.getUsersByType(2));
        return mav;
    }
    @RequestMapping("/ordertab/jy")
    public ModelAndView jy(HttpSession sess) {
        List<Ordertab> ordertabList = ordertabService.getAllOrdertab();
        List<Ordertab> ordertabList1 = new LinkedList<Ordertab>();
        for (Ordertab ordertab : ordertabList) {
			//if(sess.getAttribute("userid").toString().equals(ordertab.getUid())){

				//ordertab.setJl(goodsService.getGoodsById(Integer.parseInt(ordertab.getPid())).getProduct_name());
				ordertab.setXy(usersService.getUsersById(Integer.parseInt(ordertab.getUid())).getUsername());
				if(ordertab.getCuid()!=null&&!ordertab.getCuid().equals("")){
					ordertab.setCuid(usersService.getUsersById(Integer.parseInt(ordertab.getCuid())).getUsername());
				}

				ordertabList1.add(ordertab);
			//}
		}
        System.out.println(ordertabList.size());
        ModelAndView mav = new ModelAndView("admin/ordertab/jy");
        mav.addObject("list", ordertabList1);
        mav.addObject("list1", usersService.getUsersByType(2));
        return mav;
    }

    @Resource
    GoodsService goodsService;
    @RequestMapping("/ordertab/my1")
    public ModelAndView my1(HttpSession sess) {
        List<Ordertab> ordertabList = ordertabService.getAllOrdertab();
        List<Ordertab> ordertabList1 = new LinkedList<Ordertab>();
        for (Ordertab ordertab : ordertabList) {
			if(sess.getAttribute("userid").toString().equals(ordertab.getPid())){

				//ordertab.setJl(usersService.getUsersById(Integer.parseInt(ordertab.getPid())).getUsername());
				ordertab.setXy(usersService.getUsersById(Integer.parseInt(ordertab.getUid())).getUsername());

				ordertabList1.add(ordertab);
			}
		}
        System.out.println(ordertabList.size());
        ModelAndView mav = new ModelAndView("admin/ordertab/my1");
        mav.addObject("list", ordertabList1);
        return mav;
    }
    @RequestMapping("/ordertab/edit")
    public String ordertabEdit(String id, Model model) {
        Ordertab ordertab = ordertabService.getOrdertabById(Integer.parseInt(id));
        model.addAttribute("model", ordertab);
        return "admin/ordertab/edit";
    }
    @RequestMapping("/ordertab/settotal")
    public String settotal(String id, String total,Model model) {
        Ordertab ordertab = ordertabService.getOrdertabById(Integer.parseInt(id));
        ordertab.setTotal(total);
        ordertabService.update(ordertab);
        model.addAttribute("message", "绩效设置成功");
        return "admin/ordertab/cj";
    }



    @RequestMapping("/ordertab/shp")
    public String shp(Integer id, String score,Model model) {
        Tests t=testsService.getTestsById(id);
        t.setScore(score);
        testsService.update(t);
        model.addAttribute("message", "审批成功");
        return "admin/ordertab/qj";
    }
    @RequestMapping("/ordertab/shp1")
    public String shp1(Integer id, String score,Model model) {
        Tests t=testsService.getTestsById(id);
        t.setScore(score);
        testsService.update(t);
        model.addAttribute("message", "审批成功");
        return "admin/ordertab/jb";
    }
    @RequestMapping("/ordertab/setzg")
    public String setzg(String id, String content,Model model) {
        Ordertab ordertab = ordertabService.getOrdertabById(Integer.parseInt(id));
        ordertab.setPj(content);
        ordertabService.update(ordertab);
        model.addAttribute("message", "评价成功");
        return "admin/ordertab/zg";
    }
    @RequestMapping("/ordertab/setjy")
    public String setjy(String id, String content,Model model) {
        Ordertab ordertab = ordertabService.getOrdertabById(Integer.parseInt(id));
        ordertab.setCuid(content);
        ordertabService.update(ordertab);
        model.addAttribute("message", "就业数据更新成功");
        return "admin/ordertab/jy";
    }
    @RequestMapping("/ordertab/setpj")
    public String setpj(String id, String content,Model model) {
        Ordertab ordertab = ordertabService.getOrdertabById(Integer.parseInt(id));
        ordertab.setContent(content);
        ordertabService.update(ordertab);
        model.addAttribute("message", "评价设置成功");
        return "admin/ordertab/pj";
    }
    @RequestMapping(value = "/ordertab/update", method = RequestMethod.POST)
    public String ordertabUpdate(Integer n1,
    		Integer n2,
    		Integer n3,
    		Integer n4,
                                 int id,
                                 Model model) {

        Ordertab modelX = ordertabService.getOrdertabById(id);
        modelX.setN1(n1);
        modelX.setN2(n2);
        modelX.setN3(n3);
        modelX.setN4(n4);

        ordertabService.update(modelX);
        model.addAttribute("inpost", true);
        model.addAttribute("model", modelX);
        model.addAttribute("message", "Success");
        return "admin/ordertab/edit";
    }

    @RequestMapping(value = "/ordertab/pay", method = RequestMethod.GET)
    public String pay(
                                 int id,
                                 Model model) {

        Ordertab modelX = ordertabService.getOrdertabById(id);
        modelX.setStep("2");

        ordertabService.update(modelX);
        model.addAttribute("inpost", true);
        model.addAttribute("model", modelX);
        model.addAttribute("message", "Success");
        return "admin/ordertab/my";
    }

    @RequestMapping(value = "/ordertab/finish", method = RequestMethod.GET)
    public String finish(
                                 int id,
                                 Model model) {

        Ordertab modelX = ordertabService.getOrdertabById(id);
        modelX.setStep("3");
        modelX.setN1(100);
        modelX.setN2(100);
        modelX.setN3(100);
        modelX.setN4(100);

        ordertabService.update(modelX);
        model.addAttribute("inpost", true);
        model.addAttribute("model", modelX);
        model.addAttribute("message", "Success");
        return "admin/ordertab/my1";
    }
}
